package com.example.android_pti_backup;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class DaftarPesanan extends Activity {

	String nama_makanan, jumlah_makanan;
	TextView namaMakanan, jumlahMakanan;

	String [] list;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_daftar_pesanan);

		Bundle b = getIntent().getExtras();
		list = getSharedPreferences("p", 0).getString("list", "").split(";");
		
		namaMakanan = (TextView) findViewById(R.id.namaMakanan);
		
		jumlahMakanan = (TextView) findViewById(R.id.jumlahMakanan);
		
		nama_makanan = b.getString("nama_makanan");
		jumlah_makanan = b.getString("jumlah_makanan");
		
		namaMakanan.setText(nama_makanan);
		jumlahMakanan.setText(jumlah_makanan);
	}
	
	public void kirim(){
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				for (int i=0; i<list.length; i++){
					koneksi(list[i]);
				}
				
				getSharedPreferences("p", 0).edit().clear();
			}
		}).start();
	}
	
	private void koneksi (String list){
		try {

			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost("http://10.0.2.2/login.php");
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
			String [] data = list.split(" ");
			String query = "insert into pesan values ()";
			nameValuePairs.add(new BasicNameValuePair("query", query));
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

			HttpResponse response = httpclient.execute(httppost);
			
		} catch (Exception e) {
			System.out.println("Exception : " + e.getMessage());
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.daftar_pesanan, menu);
		return true;
	}

}
